/**
 * 作者：姚俊
 * 功能：创新团队的业务处理
 * 日期：2015-05-21
 */
package admin.service.subject;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import admin.bean.Innovations;
import beanDao.MapToBean;
import database.dao.MysqlCrud;


/**
 * @category 创新团队的业务逻辑
 * @author yaojun
 *
 */
public class InnovationService {
	
	/**
	 * @category 根据创新团队项目的ID来查询一个创新团队
	 * @author yaojun
	 * @param innovations Innovations 传入一个创新团队信息的对象，该对象必须有创新团队信息的ID
	 * @return Innovations 返回的是一个创新团队信息的对象
	 */
	public Innovations getInnovations(Innovations innovations)
	{
		Innovations inn =new Innovations();
		String sql="select * from Innovations where innId=?";
		String[]  parameter={String.valueOf(innovations.getInnId())};
        MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql, parameter);
		if(lists.size()>0&&lists!=null)
		{	
			Map map=lists.get(0);
			inn=new MapToBean().MapToInnovations(map);
		}
		
		return inn;
	}
	/**
	 * @category 根据创新团队的ID来删除一个创新团队记录
	 * @author yaojun
	 * @param innovations Innovations 传入一个创新团队信息的对象,该对象必须有创新团队的主键ID
	 * @return boolean 当删除成功返回true，否则返回false
	 */
	public boolean deleteInnovations(Innovations innovations)
	{
		boolean b=false;
		String sql="delete from Innovations where innId=?";
		String[]  parameter={String.valueOf(innovations.getInnId())};
	    MysqlCrud mysqlcurd=new MysqlCrud();
		b=mysqlcurd.InsertAndUpdateData(sql, parameter);
		 return b;
	}
	
	/**
	 * @category 插入一个创新团队信息对象数据
	 * @author yaojun
	 * @param Innovations Innovations 传入一个创新团队信息对象的参数,该对象中的数据最好都有
	 * @return boolean 如果成功true
	 */
	public boolean insertInnovations(Innovations innovations)
	{
		boolean b=false;
		String sql="insert into Innovations(name,level," +
		"superPerson,approvedDate,subjectId,classify)"+" values(?,?,?,?,?,?);";
		String[] parameter={String.valueOf(innovations.getName()),
				String.valueOf(innovations.getLevel()),String.valueOf(innovations.getSuperPerson()),
				String.valueOf(innovations.getApprovedDate()),String.valueOf(innovations.getSubjectId()),
				String.valueOf(innovations.getClassify())};	
		MysqlCrud mysqlcurd=new MysqlCrud();
		b=mysqlcurd.InsertAndUpdateData(sql, parameter);
		return b;
	}
	
	/**
	 * @category 根据创新团队信息的ID来修改这个创新团队的信息
	 * @author yaojun
	 * @param inn1 Innovations 传入一个要用于修改的Innovations对象
	 * @param inn2 Innovations 传入一个根据这个Innovations对象的ID来查找需要修改的哪一条创新团队信息数据
	 * @return boolean 成功则返回true
	 */
	public boolean updateInnovations(Innovations inn1,Innovations inn2)
	{
		boolean b=false;
		String sql="update Innovations set name=?,level=?," +
		"superPerson=?,approvedDepartment()=?,approvedDate=?," +
		"subjectId=?,classify=? where innId=?";
		
		String[] parameter={String.valueOf(inn1.getName()),
				String.valueOf(inn1.getLevel()),String.valueOf(inn1.getSuperPerson()),
				String.valueOf(inn1.getApprovedDepartment()),String.valueOf(inn1.getApprovedDate()),
				String.valueOf(inn1.getSubjectId()),String.valueOf(inn1.getClassify()),String.valueOf(inn2.getInnId())};
		
		MysqlCrud mysqlcurd=new MysqlCrud();
		b=mysqlcurd.InsertAndUpdateData(sql, parameter);
		return b;
	}
	
	/**
	 * @category 得到创新团队信息的数量
	 * @author yaojun
	 * @return int
	 */
	public int getCountInnovations()
	{
		int sum=0;
		String sql="select count(*) from Innovations";
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql,null);
		Map map=lists.get(0);
		sum=Integer.parseInt(map.get(0).toString());
		return sum;
	}
	
	
	/**
	 * @category 根据分类的级别得到创新团队信息的数量
	 * @author yaojun
	 * @param level int 按不同的级别查询不同级别的创新团队的数量，0：国家自然基金委创新群体、
     *:1：教育部创新团队:2：省重点创新团队:3：国家基金委创新群体、
     *:4：2011协同创新中心等省部级以上创新团队:5：省高校创新团队”，
     *:6：校级协同创新中心
	 * @return int
	 */
	public int getCountInnovationsByLevel(int level)
	{
		int sum=0;
		String sql="select count(*) from Innovations where level=?";
		MysqlCrud mysqlcurd=new MysqlCrud();
		String[] parameter={String.valueOf(level)};
		List<Map> lists=mysqlcurd.SelectData(sql,parameter);
		Map map=lists.get(0);
		sum=Integer.parseInt(map.get(0).toString());
		return sum;
	}
	
	/**
	 * @category 根据名称的条件查询所有创新团队信息
	 * @author yaojun
	 * @param Innovations Innovations 传入一个创新团队管理信息的对象，该对象中必须有创新团队管理信息的名称
     * @return 返回一个类型为Innovations的List集合
	 */
	public List<Innovations> getAllInnovationsByName(Innovations innovations)
	{
		String sql="select * from Innovations where name like '%"+innovations.getName()+"%'";
		List<Innovations> innovationss=new ArrayList<Innovations>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql,null);
		for(int i=0;i<lists.size();i++)
		{
			Map map=lists.get(i);
			innovationss.add(new MapToBean().MapToInnovations(map));
		}
		return innovationss;		
	}	
	
	/**
	 * @category 查询所有创新团队信息
	 * @author yaojun
     * @return 返回一个类型为Innovations的List集合
	 */
	public List<Innovations> getAllInnovations()
	{
		String sql="select * from Innovations order by level,classify";
		List<Innovations> innovationss=new ArrayList<Innovations>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql,null);
		for(int i=0;i<lists.size();i++)
		{
			Map map=lists.get(i);
			innovationss.add(new MapToBean().MapToInnovations(map));
		}
		return innovationss;		
	}	
}
